<template>
  <div class="Register_container">
    <div class="Register_box">
      <div style="padding:20px;background-color: white;margin-left: 100px;border-radius: 20px;box-shadow:0 0  10px rgba(0,0,0.1)">
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width:400px" >
          <div style="margin-bottom:30px;font-size:20px;color:#0742b1;font-weight:bolder;text-align: center">欢迎注册后台管理系统</div>
          <el-form-item style="margin-top: 20px;" prop="username">
            <el-input size="large" autocomplete="off" v-model="data.form.username" placeholder="请输入账号" prefix-icon="User" >
            </el-input>
          </el-form-item>
          <el-form-item  prop="password">
            <el-input show-password="show-password" autocomplete="off" size="large" v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock">
            </el-input>

          </el-form-item>
          <el-form-item  prop="confirmPassword">
            <el-input show-password="show-password" autocomplete="off" size="large" v-model="data.form.confirmPassword" placeholder="请再次输入密码" prefix-icon="Lock">
            </el-input>
            {{data.form.confirmPassword}}
          </el-form-item>
          <div>
            <el-button size="large" style="width: 48%" type="primary" @click="register()">注 册</el-button>
            <el-button size="large" style="width: 48%" type="info">取 消</el-button>
          </div>
          <div style="text-align: right;margin-top: 20px;">
            已有账号？请<a style="color: #0742b1;text-decoration: none;" href="/login">登录</a>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive,ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const res = ref()


const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请再次输入密码'))
  } else if (value !== data.form.password) {
    callback(new Error("两次输入密码不一致!"))
  } else {
    callback()
  }
}
const data = reactive({
  form:{
    username:'',
    password:'',
    confirmPassword:''
  },
  rules:{
    username:[{required:true,message:'请输入账号',trigger:'blur'}],
    password:[{required:true,message:'请输入密码',trigger:'blur'}],
    confirmPassword:[{validator:validatePass,trigger:'blur'}]
  }
})
const formRef = ref()
const register = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/register', data.form)
          .then(res => {
            if (res.code === '200') {

              ElMessage.success('注册成功');
              location.href = '/login'

            } else {
              ElMessage.error(res.msg); // 显示业务错误消息
            }
          })
          .catch(error => {
            if (error.response) {
              // 处理 HTTP 错误状态码（如 400）
              const { data } = error.response;
              if (data && data.msg) {
                ElMessage.error(data.msg); // 显示后端返回的错误消息
              } else {
                ElMessage.error('请求失败，请稍后重试');
              }
            } else {
              ElMessage.error('网络请求失败，请检查网络连接');
            }
          });
    }
  });
}




</script>
<!--scoped表示当页生效-->
<style scoped>
.Register_container{
  height: 100vh;
  overflow:hidden;
  background-image: url("@/assets/login_bg.png");
  background-size:cover ;
  background-position: 0 -40px;
}
.Register_box{
  width: 50%;
  height: 100%;
  display: flex;
  align-items: center;
  right: 0;
  position: absolute;
}
</style>
